# frozen_string_literal: true

require_relative '../../modules/abilities/countable'

class Pagy
  module OffsetPaginator
    module_function

    # Return the Pagy::Offset instance and results
    def paginate(collection, options)
      options[:page]  ||= options[:request].resolve_page
      options[:limit]   = options[:request].resolve_limit
      options[:count] ||= Countable.get_count(collection, options)
      pagy = Offset.new(**options)
      [pagy, collection.instance_of?(Array) ? collection[pagy.offset, pagy.limit] : pagy.records(collection)]
    end
  end
end
